import Vue from 'vue'
import Router from 'vue-router'
import Home from './views/Home.vue'
import UserLayout from './layouts/UserLayout.vue'
// import DataList from './views/dataList'
// import TableList from './views/tableList'

Vue.use(Router)

const router = new Router({
  mode: 'history',
  base: process.env.BASE_URL,
  routes: [
    {
      path: '/',
      name: 'home',
      component: Home,
        meta: {
            requireAuth:true,
            title: '首页'
        },
      children: []
    },
    {
      path: '/user',
      name: 'user',
      component: UserLayout,
      meta: {
          requireAuth: false,
          title: '用户'
      },
        redirect: {name: 'login'},   //默认页面
      children: [
          {
              path : '/login',
              component: () => import(/* webpackChunkName: "about" */ './views/user/login.vue'),
              name: 'login',
              meta: {
                  requireAuth: false,
                  title: '登录'
              }
          },
      ]
    }
    // {
    //   path: '/login',
    //   name: 'login',
    //   // component: Home,
    //     component: () => import(/* webpackChunkName: "about" */ './views/user/login.vue'),
    //   meta: {
    //       requireAuth:false,
    //       title: '登录'
    //   },
    //   children: []
    // }
  ]
});

router.beforeEach((to, from, next) => {
    if (to.meta.requireAuth) { // 判断该路由是否需要登录权限
        if(localStorage.getItem('token')){ //判断本地是否存在token
            next();
        }else {
            next({
                path:'/login'
            })
        }
    }
    else {
        next();
    }
    /*如果本地 存在 token 则 不允许直接跳转到 登录页面*/
    if(to.fullPath == "/login"){
        if(localStorage.getItem('token')){
            next({
                path:from.fullPath
            });
        }else {
            next();
        }
    }
});

export default router;
